package com.wanmait.houlang.config;

//import com.wanmait.houlang.interceptor.JWTInterceptor;
import com.wanmait.houlang.interceptor.JWTInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private JWTInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(jwtInterceptor).addPathPatterns("/manage/**").excludePathPatterns("/manage/login");
        //如果不排除可以在login方法上加@TokenIgnore注解
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/manage/**");
    }
    //图片存放路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //registry.addResourceHandler("/uploadImages/**").addResourceLocations("file:D:/uploadImages/");
        registry.addResourceHandler("/static/img/schoolLogo/**").addResourceLocations("classpath:/static/img/schoolLogo/");
    }
//全局跨域
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")  // 匹配了所有的URL
//                .allowedHeaders("*")  // 允许跨域请求包含任意的头信息
//                .allowedMethods("\"GET\", \"HEAD\", \"POST\", \"PUT\", \"DELETE\", \"OPTIONS\"")  // 设置允许的方法
//                .allowedOrigins("*")  // 设置允许跨域请求的域名
//                .allowCredentials(true);  // 是否允许证书，默认false
//    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true) .maxAge(3600)  .allowedHeaders("*");

    }
}
